package com.abase.site.client.open.widget.frontpage;



import com.abase.site.client.PocAbaseHistoryHandler.HistoryTokens;
import com.abase.site.client.services.YAMLLoadingService;
import com.abase.site.client.services.YAMLLoadingServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class FrontPageWidget extends Composite implements ValueChangeHandler<String> {

	private static FrontPageWidgetUiBinder uiBinder = GWT
			.create(FrontPageWidgetUiBinder.class);

	interface FrontPageWidgetUiBinder extends UiBinder<Widget, FrontPageWidget> {
	}

	public FrontPageWidget() throws Exception{
		initWidget(uiBinder.createAndBindUi(this));

//		YAMLLoadingServiceAsync service = GWT.create (YAMLLoadingService.class);
//		final AsyncCallback<String> callback = new AsyncCallback<String>(){
//			@Override
//			public void onFailure(Throwable caught) {
//				// TODO Auto-generated method stub
//			}
//			@Override
//			public void onSuccess(String result) {
//				// TODO Auto-generated method stub
//			}
//		};
//		service.loadingYamlFileService("frontPageContenu", callback);
		//cette ligne permet d'être à l'écoute de l'hitory event
		History.addValueChangeHandler(this);
	}

	@UiField Label linkLabel;

	@UiHandler("linkLabel")
	void onClick(ClickEvent e) {
		History.newItem(HistoryTokens.HISTORY_TEST_TOKEN.getValue());
	}

  @Override
  public void onValueChange(ValueChangeEvent<String> event) {
    
    if (event.getValue().equals(HistoryTokens.HOME_TOKEN.getValue())) {
      //TODO action quand on recupere le token qui interesse notre widget, dans ce cas, le home token
      Window.alert("History catching events, token value = " + event.getValue());
    }
    
  }

}
